package com.uottawa.library.controller;

import java.io.IOException;
import java.util.List;
 
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.uottawa.library.domain.Loan;
import com.uottawa.library.domain.User;
import com.uottawa.library.service.LoanService;
 
/**
 * Servlet implementation class LoginServlet
 */
@WebServlet(name="/PaymentServlet",  urlPatterns={"/payfees.do"})

public class PaymentServlet extends HttpServlet {

	private static final long serialVersionUID = 1L;
	private LoanService loanService = new LoanService();
	
    protected void doPost(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {
    	String loanId = request.getParameter("id");
    	loanService.payFees(Integer.parseInt(loanId));
    	request.getRequestDispatcher("/payfine.jsp").forward(request, response);

 
    }
    
    protected void doGet(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {
    	
    	User user = (User) request.getSession().getAttribute("user");
    	
    	List<Loan> items = loanService.getLoansWithFeesForUser(user.getUserId());
		request.setAttribute("items", items);	
    	request.getRequestDispatcher("/payfine.jsp").forward(request, response);

 
    }
 
}